package com.dai.tools;

import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;

/**
 * 主要用来解决几个时间相关的问题:
 * 1. 根据  时间戳  获得 显示的时间,如: 2020-01-01 22:23:12
 */
public class MyTimeUtils {

    /**
     *
     * @param time 时间戳
     * @return 如: 2020-01-01 22:23:12
     */
    public static String fullString(long time){
        SimpleDateFormat ymdhmsSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return ymdhmsSdf.format(new Date(time));
    }

    /**
     *
      * @param time 时间戳
     * @return 如: 2020-01-01
     */
    public static String dayTimeString(long time){
        SimpleDateFormat daytimeSdf = new SimpleDateFormat("yyyy-MM-dd");
        return  daytimeSdf.format(new Date(time));
    }

    /**
     *
     * @param time 时间戳
     * @return 如: 23:36:34
     */
    public static String timeString(long time){
        SimpleDateFormat timeSdf = new SimpleDateFormat("HH:mm:ss");
        return  timeSdf.format(new Date(time));
    }

    /**
     *
     * @param time 时间戳
     * @return 如: 36:34
     */
    public static String minutesAndSeconds(long time){
        SimpleDateFormat minutesAndSecondSdf = new SimpleDateFormat("mm:ss");
        return minutesAndSecondSdf.format(time);
    }

    /**
     * 格式化显示 mm:ss
     * @param betweenMs  如:  713000ms
     * @return  如: 11:53
     */
    public static String format_mmss(long betweenMs){
        StringBuilder sb = new StringBuilder();
        int minutes = (int) (betweenMs / (60 * 1000));
        int seconds = (int) ((betweenMs - minutes * (60 * 1000)) / 1000);
        if(minutes < 10){
            sb.append("0");
        }
        sb.append(minutes);
        sb.append(":");
        if(seconds < 10){
            sb.append("0");
        }
        sb.append(seconds);
        return sb.toString();
    }

    /**
     *  格式化显示 HH:mm:ss
     * @param betweenMs
     * @return 如:01:11:53
     */
    public static String format_HHmmss(long betweenMs){
        StringBuilder sb = new StringBuilder();
        int hour = (int) (betweenMs / (3600 * 1000));
        long leftMs = betweenMs - hour * (3600 * 1000);
        int minutes = (int) (leftMs / (60 * 1000));
        int seconds = (int) ((leftMs - minutes * (60 * 1000)) / 1000);
        if(hour < 10){
            sb.append("0");
        }
        sb.append(hour);
        sb.append(":");
        if(minutes < 10){
            sb.append("0");
        }
        sb.append(minutes);
        sb.append(":");
        if(seconds < 10){
            sb.append("0");
        }
        sb.append(seconds);
        return  sb.toString();
    }

    /**
     * 根据 stamp 时间所在的日期, 返回 当天 hour:minutes:seconds 时候的 stamp
     * @param stamp
     * @param hour
     * @param minutes
     * @param seconds
     * @return
     */
    public static long getSpecifiedStampByStamp(long stamp,int hour,int minutes,int seconds){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(stamp);
        int h = calendar.get(Calendar.HOUR_OF_DAY);
        int m = calendar.get(Calendar.MINUTE);
        int s = calendar.get(Calendar.SECOND);
        int ms = calendar.get(Calendar.MILLISECOND);
        System.out.println("h:" + h);
        System.out.println("m:" + m);
        System.out.println("s:" + s);
        long startOfday = stamp - (h * 3600  + m * 60 + s) * 1000 - ms;
        Instant instant = Instant.ofEpochMilli(startOfday);
        return instant.plusMillis(hour * 3600 * 1000 + minutes * 60 * 1000 + seconds * 1000).toEpochMilli();
    }
    public static long getSpecifiedStampByStamp(long stamp,int hour,int minutes){
        return  getSpecifiedStampByStamp(stamp,hour,minutes,0);
    }
    public static long getSpecifiedStampByStamp(long stamp,int hour){
        return  getSpecifiedStampByStamp(stamp,hour,0,0);
    }

//    public static void main(String[] args) {
//
//        long time = System.currentTimeMillis();
//        System.out.println("time:" + time);
//        long timeNext = MyTimeUtils.getSpecifiedStampByStamp(time,22,10,23);
//        System.out.println("timeNext:" + timeNext);
//
////        LocalDateTime localDateTime = LocalDateTime.of(2021,1,11,22,10,23);
////        long seconds = localDateTime.toEpochSecond(ZoneOffset.UTC);
////        System.out.println("seconds:" + seconds * 1000);
//
//        ZonedDateTime zonedDateTime = ZonedDateTime.of(2021,1,11,
//                22,10,23,0,ZoneId.systemDefault());
//        Instant ins = zonedDateTime.toInstant();
//        System.out.println(ins.toEpochMilli());
//
//        String zoneId = ZoneId.systemDefault().getId();
//        System.out.println("zoneId:" + zoneId);
//
//        LocalDateTime localDateTime = LocalDateTime.of(2021,1,11,22,10,23);
//        Instant ins2 = localDateTime.toInstant(ZoneOffset.of("+08:00"));
//        System.out.println("ins2:" + ins2.toEpochMilli());
//    }


}
